package com.zys.el.user.controller;

import com.zys.el.core.controller.BaseController;
import com.zys.el.core.po.ResponseBean;
import com.zys.el.user.po.Tools;
import com.zys.el.user.service.IToolsService;
import com.zys.el.user.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/tools")
public class ToolsController extends BaseController<IToolsService, Tools> {

    @Autowired
    private IUserService userService;

    @GetMapping("/look/{uid}")
    @ApiOperation(value = "当前用户能看到的工具列表", notes = "")
    public ResponseBean getUsersLookTools(@PathVariable Long uid) {
        ResponseBean rn = new ResponseBean();
        List<Tools> toolsByUserId = service.getToolsByUserId(uid);

        Map<String, List<Tools>> re = new HashMap<>();
        for (Tools tools : toolsByUserId) {
            String category = tools.getCategory();
            List<Tools> tools1 = re.get(category);
            if (tools1 == null) {
                // 如果第一次插入
                tools1 = new ArrayList<>();
            }
            tools1.add(tools);
            re.put(category, tools1);
        }


        rn.setModel(re);
        rn.setMsg("查询成功");
        rn.setSuccess(true);
        return rn;
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加工具", notes = "权限只能设置为0 和 1")
    public ResponseBean saveTools(@RequestBody Tools tools) {
        ResponseBean rn = new ResponseBean();
        rn.setCode(500);
        rn.setSuccess(false);
        if (tools.getDataUrl() == null) {
            rn.setMsg("链接不能空");
            return rn;
        } else if (tools.getTitle() == null) {
            rn.setMsg("标题不能空");
            return rn;
        } else if (tools.getOwnerId() == null) {
            rn.setMsg("创作者不能空");
            return rn;
        } else if (tools.getOwnerId() != null && userService.getById(tools.getOwnerId()) == null) {
            rn.setMsg("创作者不存在");
            return rn;
        } else {
            boolean b = service.saveOrUpdate(tools);
            if (b == true) {
                rn.setCode(200);
                rn.setMsg("保存成功");
                rn.setModel(tools);
            } else {
                rn.setMsg("服务器错误：保存失败");
                return rn;
            }
        }
        return rn;
    }

    @PostMapping("/auth")
    @ApiOperation(value = "用户授权", notes = "为一组用户授权")
    public ResponseBean AuthUsersByToolId(@RequestBody Tools tools) {
        ResponseBean rn = new ResponseBean();
        rn.setCode(400);
        rn.setSuccess(false);
        if (tools.getId() == null) {
            rn.setMsg("工具id为空");
            return rn;
        }
        boolean b = service.AuthToUsers(tools.getId(), tools.getOwners());
        if (b == false) {
            rn.setCode(500);
            rn.setMsg("服务器错误");
            return rn;
        }
        rn.setCode(200);
        rn.setModel(tools);
        rn.setSuccess(true);
        rn.setMsg("授权成功");
        return rn;

    }



}
